﻿using System;
using System.Collections.Generic;
using LoungeRepo.Contracts.Normalizer;

namespace LoungeRepo.Normalizer
{
    partial class ObjectGraphDenormalizer
    {
        private object CreateDenormalizedObject(INormalizedObject normalizedObject)
        {
            var denormalizedObject = Activator.CreateInstance(normalizedObject.ObjectType);

            this.denormalizedObjectCache.Add(normalizedObject.RefId, denormalizedObject);
            return denormalizedObject;
        }


        private Array CreateDenormalizedArray(INormalizedObject normalizedObject, IList<INormalizedField> normalizedFields)
        {
            int rank = (int)normalizedFields[0].Value;

            List<int> dimLengths = new List<int>();
            for (int dimension = 0; dimension < rank; dimension++)
                dimLengths.Add((int)normalizedFields[1 + dimension].Value);

            var denormalizedArray = Array.CreateInstance(normalizedObject.ObjectType.GetElementType(), dimLengths.ToArray());

            this.denormalizedObjectCache.Add(normalizedObject.RefId, denormalizedArray);
            return denormalizedArray;
        }
    }
}
